<?php 
namespace app\model;
use think\Model;
use think\Db;

class Integral extends Model
{	
    public static function getTypeCn($key){
        $list = [1=>'邀请',2=>'签到',3=>'兑换',10=>'系统处理'];
        return $list[$key];
    }

    public static function getTypeList(){
        $list = [1=>'邀请',2=>'签到',3=>'兑换',10=>'系统处理'];
        return $list;
    }

    public static function saveData($data=[]){
        if (empty($data['amount'])) {
            return false;
        }
        $data['ctime'] = time();
        if ( !isset($data['old_amount']) ) {
            $data['old_amount'] = Db::name('user')->where('id',$data['user_id'])->value('integral');
        }
        $data['new_amount'] = $data['old_amount'] + $data['amount'];
        if ( isset($data['param']) ) {
            // $data['param'] = json_encode($data['param']);
        }
        // 添加账户记录
        if ( !self::insert($data) ) { return false; }
        // 给用户增加余额
        if ( !Db::name('user')->where('id',$data['user_id'])->inc('integral',$data['amount'])->update() ) { return false; }
        return true;
    }
}


 ?>